#pragma once
#include "SDL.h" 
#include <iostream>
#include "hitQueue.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 20; 
const int VELOCITY = 200;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *playerIMG = NULL;
SDL_Surface *monsterIMG = NULL;
SDL_Surface *potion = NULL;
SDL_Surface *gold = NULL;
SDL_Event event; 
using namespace std;
TTF_Font *fontA = NULL; 
SDL_Color textColor = { 255, 255, 255 }; 
SDL_Surface *load_image( std::string filename ) 
{
	//Temporary storage for the image that's loaded 
	SDL_Surface* loadedImage = NULL; 
	//The optimized image that will be used 
	SDL_Surface* optimizedImage = NULL; 
	//Load the image 
	loadedImage = SDL_LoadBMP( filename.c_str() ); 
	//If nothing went wrong in loading the image 
	if( loadedImage != NULL ) 
	{ 
		//Create an optimized image 
		optimizedImage = SDL_DisplayFormat( loadedImage ); 
		//Free the old image 
		SDL_FreeSurface( loadedImage ); 
		//If the image was optimized just fine 
		if( optimizedImage != NULL ) 
		{ 
			//Map the color key 
			Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ); 
			//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent 
			SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey ); 
		}
	} 
	//Return the optimized image 
	return optimizedImage; 
};
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ) 
{
	//Make a temporary rectangle to hold the offsets 
	SDL_Rect offset;
	//Give the offsets to the rectangle 
	offset.x = x;
	offset.y = y;
	//Blit the surface 
	SDL_BlitSurface( source, clip, destination, &offset ); 
}
bool init()
{
	//Initialize all SDL subsystems
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
		return false;
	//Ser up the screen
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE ); 
	if(screen == NULL)
		return false;
	if( TTF_Init() == -1 ) { return false; } 
	//set the window caption
	SDL_WM_SetCaption( "Venture Land!", NULL );
	//If everything initialized fine
	return true;
}
bool load_files() 
{ 
	//Load the image 
	background = load_image("background.bmp");
	playerIMG = load_image("Donovan.bmp");
	monsterIMG = load_image("Donovan1.bmp");
	potion = load_image("potion.bmp");
	gold = load_image("gold.bmp");
	fontA = TTF_OpenFont( "arialbd.ttf", 28 ); 
	//If everything loaded fine 
	return true; 
} 

void clean_up()
{
	// free the image
	SDL_FreeSurface(background);
	SDL_FreeSurface(playerIMG);
	SDL_FreeSurface(potion);
	SDL_FreeSurface(gold);
	TTF_CloseFont( fontA ); 
	//Quit SDL
	TTF_Quit();
	SDL_Quit();
}

bool chkCol(SDL_Rect A, SDL_Rect B)
{
	int leftA, leftB;
	int rightA, rightB;
	int topA, topB;
	int bottomA, bottomB; 

	leftA = A.x;
	rightA = A.x + A.w; 
	topA = A.y; 
	bottomA = A.y + A.h; 
	
	leftB = B.x;
	rightB = B.x + B.w; 
	topB = B.y; 
	bottomB = B.y + B.h; 

	if(leftA < rightB && rightA > leftB && topA < bottomB && bottomA > topB)
	{
		return true;
	}
	return false;
}

class Timer
{
    private:
    //The clock time when the timer started
    int startTicks;

    //The ticks stored when the timer was paused
    int pausedTicks;

    //The timer status
    bool paused;
    bool started;

    public:
    //Initializes variables
    Timer();

    //The various clock actions
    void start();
    void stop();
    void pause();
    void unpause();

    //Gets the timer's time
    int get_ticks();

    //Checks the status of the timer
    bool is_started();
    bool is_paused();
};
Timer::Timer()
{
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}

void Timer::start()
{
    //Start the timer
    started = true;

    //Unpause the timer
    paused = false;

    //Get the current clock time
    startTicks = SDL_GetTicks();
}

void Timer::stop()
{
    //Stop the timer
    started = false;

    //Unpause the timer
    paused = false;
}

void Timer::pause()
{
    //If the timer is running and isn't already paused
    if( ( started == true ) && ( paused == false ) )
    {
        //Pause the timer
        paused = true;

        //Calculate the paused ticks
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    //If the timer is paused
    if( paused == true )
    {
        //Unpause the timer
        paused = false;

        //Reset the starting ticks
        startTicks = SDL_GetTicks() - pausedTicks;

        //Reset the paused ticks
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the timer was paused
            return pausedTicks;
        }
        else
        {
            //Return the current time minus the start time
            return SDL_GetTicks() - startTicks;
        }
    }

    //If the timer isn't running
    return 0;
}

bool Timer::is_started()
{
    return started;
}

bool Timer::is_paused()
{
    return paused;
}
